<?php
class ControllerShelfGoodsshelf  extends Controller {
  private $error = array();

  public function index() {
    $this->load->language('sale/order');

    $this->document->setTitle($this->language->get('heading_title'));

    $this->load->model('sale/order');
    $this->load->model('sale/stock_out');

    $this->getList();
  }


  protected function getList() {
    //会员
    if (isset($this->request->get['filter_customer'])) {
      $filter_customer = $this->request->get['filter_customer'];
    } else {
      $filter_customer = null;
    }
     //推荐码
    if (isset($this->request->get['filter_recommended_code'])) {
      $filter_recommended_code = $this->request->get['filter_recommended_code'];
    } else {
      $filter_recommended_code = null;
    }
    //查询购买开始时间
    if (isset($this->request->get['filter_date_start'])) {
      $filter_date_start = $this->request->get['filter_date_start'];
    } else {
      $filter_date_start = null;
    }

    //查询上架开始时间
    if (isset($this->request->get['filter_date_end'])) {
      $filter_date_end = $this->request->get['filter_date_end'];
    } else {
      $filter_date_end = null;
    }
  
    if (isset($this->request->get['sort'])) {
      $sort = $this->request->get['sort'];
    } else {
      $sort = 'o.date_added';
    }

    if (isset($this->request->get['order'])) {
      $order = $this->request->get['order'];
    } else {
      $order = 'DESC';
    }

    if (isset($this->request->get['page'])) {
      $page = $this->request->get['page'];
    } else {
      $page = 1;
    }

    $url = '';
  
    //会员
    if (isset($this->request->get['filter_customer'])) {
      $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
    }
    //推荐码
    if (isset($this->request->get['filter_recommended_code'])) {
      $url .= '&filter_recommended_code=' . $this->request->get['filter_recommended_code'];
    }

    //查询购买时间
    if (isset($this->request->get['filter_date_start'])) {
      $url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
    }

    //查询上架时间
    if (isset($this->request->get['filter_date_end'])) {
      $url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
    }

    if (isset($this->request->get['sort'])) {
      $url .= '&sort=' . $this->request->get['sort'];
    }

    if (isset($this->request->get['order'])) {
      $url .= '&order=' . $this->request->get['order'];
    }
/*
    if (isset($this->request->get['page'])) {
      $url .= '&page=' . $this->request->get['page'];
    }
*/
    $data['breadcrumbs'] = array();

    $data['breadcrumbs'][] = array(
      'text' => $this->language->get('text_home'),
      'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
    );

    $data['breadcrumbs'][] = array(
      'text' => $this->language->get('heading_title'),
      'href' => $this->url->link('sale/order', 'token=' . $this->session->data['token'] . $url, 'SSL')
    );

    $data['invoice'] = $this->url->link('sale/order/invoice', 'token=' . $this->session->data['token'], 'SSL');
    $data['shipping'] = $this->url->link('sale/order/shipping', 'token=' . $this->session->data['token'], 'SSL');
    $data['add'] = $this->url->link('sale/order/add', 'token=' . $this->session->data['token'], 'SSL');
    $data['exportReceiptWithOutPay'] = $this->url->link('sale/order/exportReceiptWithOutPay', 'token=' . $this->session->data['token'], 'SSL');

    $data['orders'] = array();

    $filter_data = array(
      'filter_customer'      => $filter_customer,//会员
      'filter_recommended_code'         => $filter_recommended_code,//推荐码
      'filter_date_start'  => $filter_date_start, //购买开始时间
      'filter_date_end' => $filter_date_end,    //上架开始时间
      'sort'                 => $sort,
      'order'                => $order,
      'start'                => ($page - 1) * $this->config->get('config_limit_admin'),
      'limit'                => $this->config->get('config_limit_admin')
    );
    //货架方法..................
    $this->load->model('shelf/goodsshelf');
    $sql=$this->model_shelf_goodsshelf->getorder($filter_data);

    $order_total = $this->model_shelf_goodsshelf->getTotalOrders($filter_data);
  //print_r($order_total);die;
    //print_r($sql);die;
    $data['orders'] = $sql;
    $data['heading_title'] = "货架";
    $data['view']  =  $this->url->link('sale/order/info', 'token=' . $this->session->data['token'] . $url, 'SSL');
    $data['edit']  =  $this->url->link('shelf/goodsshelf/edit', 'token=' . $this->session->data['token'].$url, 'SSL');
    $data['text_list'] = "货架列表";
    $data['text_no_results'] = $this->language->get('text_no_results');
    $data['text_confirm'] = $this->language->get('text_confirm');
    $data['text_missing'] = $this->language->get('text_missing');
    $data['text_loading'] = $this->language->get('text_loading');

    $data['column_order_id'] = "货架ID";
    $data['column_customer'] = "会员";;
    $data['column_status'] = $this->language->get('column_status');
    $data['column_total'] = $this->language->get('column_total');
    $data['column_date_added'] = $this->language->get('column_date_added');
    $data['column_date_modified'] = $this->language->get('column_date_modified');
    $data['column_action'] = $this->language->get('column_action');

    $data['entry_return_id'] = $this->language->get('entry_return_id');
    $data['entry_order_id'] = $this->language->get('entry_order_id');
    $data['entry_customer'] = $this->language->get('entry_customer');
    $data['entry_order_status'] = $this->language->get('entry_order_status');
    $data['entry_total'] = $this->language->get('entry_total');
    $data['entry_date_added'] = $this->language->get('entry_date_added');
    $data['entry_date_modified'] = $this->language->get('entry_date_modified');

    $data['button_invoice_print'] = $this->language->get('button_invoice_print');
    $data['button_shipping_print'] = $this->language->get('button_shipping_print');
    $data['button_add'] = $this->language->get('button_add');
    $data['button_edit'] = $this->language->get('button_edit');
    $data['button_delete'] = $this->language->get('button_delete');
    $data['button_filter'] = $this->language->get('button_filter');
    $data['button_view'] = $this->language->get('button_view');
    $data['button_ip_add'] = $this->language->get('button_ip_add');

    $data['token'] = $this->session->data['token'];
    $data['user_group_id'] = $this->session->data['user_group_id'];
    $data['user_id'] = $this->session->data['user_id'];

    if (isset($this->request->post['selected'])) {
      $data['selected'] = (array)$this->request->post['selected'];
    } else {
      $data['selected'] = array();
    }

    if (isset($this->session->data['error'])) {
      $data['error_warning'] = $this->session->data['error'];
      unset($this->session->data['error']);
    } elseif (isset($this->error['warning'])) {
      $data['error_warning'] = $this->error['warning'];
    } else {
      $data['error_warning'] = '';
    }
    $url = '';
    //会员
    if (isset($this->request->get['filter_customer'])) {
      $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
    }
    //推荐码
    if (isset($this->request->get['filter_recommended_code'])) {
      $url .= '&filter_recommended_code=' . $this->request->get['filter_recommended_code'];
    }
 

    if (isset($this->request->get['filter_date_start'])) {
      $url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
    }
  

    if (isset($this->request->get['filter_date_end'])) {
      $url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
    }
  
    if ($order == 'ASC') {
      $url .= '&order=DESC';
    } else {
      $url .= '&order=ASC';
    }

    if (isset($this->request->get['page'])) {
      $url .= '&page=' . $this->request->get['page'];
    }

        $data['hand_order'] = $this->url->link('sale/order/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $data['sort_order'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.order_id' . $url, 'SSL');
        $data['sort_customer'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=customer' . $url, 'SSL');
        $data['sort_status'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=status' . $url, 'SSL');
        $data['sort_verify_status'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=verify_status' . $url, 'SSL');
        $data['sort_total'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.total' . $url, 'SSL');
        $data['sort_ori_total'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.ori_total' . $url, 'SSL');
        $data['sort_date_added'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.date_added' . $url, 'SSL');
        $data['sort_date_modified'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.date_modified' . $url, 'SSL');
        $data['sort_SOT'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.stock_out_rate_by_type' . $url, 'SSL');
        $data['sort_SOQ'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.stock_out_rate_by_qty' . $url, 'SSL');
        $data['sort_SOP'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.stock_out_rate_by_price' . $url, 'SSL');
        $data['sort_DT'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.delivery_rate_by_type' . $url, 'SSL');
        $data['sort_DQ'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.delivery_rate_by_qty' . $url, 'SSL');
        $data['sort_DT'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . '&sort=o.delivery_rate_by_price' . $url, 'SSL');

    $url = '';

    //会员
    if (isset($this->request->get['filter_customer'])) {
      $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
    }
    //推荐码
    if (isset($this->request->get['filter_recommended_code'])) {
      $url .= '&filter_recommended_code=' . $this->request->get['filter_recommended_code'];
    }
 
 
    if (isset($this->request->get['filter_date_start'])) {
      $url .= '&filter_date_start=' . $this->request->get['filter_date_start'];
    }
   
   
    if (isset($this->request->get['filter_date_end'])) {
      $url .= '&filter_date_end=' . $this->request->get['filter_date_end'];
    }
      

    if (isset($this->request->get['sort'])) {
      $url .= '&sort=' . $this->request->get['sort'];
    }

    if (isset($this->request->get['order'])) {
      $url .= '&order=' . $this->request->get['order'];
    }

    $pagination = new Pagination();
    $pagination->total = $order_total;
    $pagination->page = $page;
    $pagination->limit = $this->config->get('config_limit_admin');
    $pagination->url = $this->url->link('shelf/goodsshelf', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

    $data['pagination'] = $pagination->render();

    $data['results'] = sprintf($this->language->get('text_pagination'), ($order_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($order_total - $this->config->get('config_limit_admin'))) ? $order_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $order_total, ceil($order_total / $this->config->get('config_limit_admin')));


    $data['filter_customer'] = $filter_customer;
    $data['filter_date_start'] = $filter_date_start;
    $data['filter_date_end'] = $filter_date_end;
    $data['filter_recommended_code'] = $filter_recommended_code;

    $data['sort'] = $sort;
    $data['order'] = $order;
    $data['orderClass'] = ($order == 'DESC')?'ASC':'DESC';

    $data['store'] = HTTPS_CATALOG;

    // API login
    $this->load->model('user/api');

    $api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));

    if ($api_info) {
      $data['api_id'] = $api_info['api_id'];
      $data['api_key'] = $api_info['key'];
      $data['api_ip'] = $this->request->server['REMOTE_ADDR'];
    } else {
      $data['api_id'] = '';
      $data['api_key'] = '';
      $data['api_ip'] = '';
    }

    $data['can_edit'] = $this->user->hasPermission('modify', 'sale/order');

    $data['header'] = $this->load->controller('common/header');
    $data['column_left'] = $this->load->controller('common/column_left');
    $data['footer'] = $this->load->controller('common/footer');

    $this->response->setOutput($this->load->view('shelf/shelf_list.tpl', $data));
  }

  public function info() {
    $this->load->model('shelf/goodsshelf');
    $this->load->model('sale/order');
    if (isset($this->request->get['shelf_order_id'])) {
      $shelf_order_id = $this->request->get['shelf_order_id'];
    } else {
      $shelf_order_id = 0;
    }

    $order_info = $this->model_shelf_goodsshelf->getinfo($shelf_order_id);
   // print_r($order_info);die;

    if ($order_info) {
      $this->load->language('sale/order');

      $this->document->setTitle($this->language->get('heading_title'));

      $data['heading_title'] = "货架详情";

      $data['text_ip_add'] = sprintf($this->language->get('text_ip_add'), $this->request->server['REMOTE_ADDR']);
      $data['text_order_detail'] = $this->language->get('text_order_detail');
      $data['text_customer_detail'] = $this->language->get('text_customer_detail');
      $data['text_option'] = $this->language->get('text_option');
      $data['text_store'] = $this->language->get('text_store');
      $data['text_date_added'] = $this->language->get('text_date_added');
      $data['text_payment_method'] = $this->language->get('text_payment_method');
      $data['text_shipping_method'] = $this->language->get('text_shipping_method');
      $data['text_customer'] = $this->language->get('text_customer');
      $data['text_customer_group'] = $this->language->get('text_customer_group');
      $data['text_email'] = $this->language->get('text_email');
      $data['text_telephone'] = $this->language->get('text_telephone');
      $data['text_is_invoice_added'] = $this->language->get('text_is_invoice_added');
      $data['text_is_invoice_removed'] = $this->language->get('text_is_invoice_removed');
      $data['text_is_invoice_passed'] = $this->language->get('text_is_invoice_passed');
      $data['text_invoice'] = $this->language->get('text_invoice');
      $data['text_is_invoice'] = $this->language->get('text_is_invoice');
      $data['text_reward'] = $this->language->get('text_reward');
      $data['text_affiliate'] = $this->language->get('text_affiliate');
      $data['text_order'] = sprintf($this->language->get('text_order'), $this->request->get['order_id']);
      $data['text_payment_address'] = $this->language->get('text_payment_address');
      $data['text_shipping_address'] = $this->language->get('text_shipping_address');
      $data['text_comment'] = $this->language->get('text_comment');

      $data['text_account_custom_field'] = $this->language->get('text_account_custom_field');
      $data['text_payment_custom_field'] = $this->language->get('text_payment_custom_field');
      $data['text_shipping_custom_field'] = $this->language->get('text_shipping_custom_field');
      $data['text_browser'] = $this->language->get('text_browser');
      $data['text_ip'] = $this->language->get('text_ip');
      $data['text_forwarded_ip'] = $this->language->get('text_forwarded_ip');
      $data['text_user_agent'] = $this->language->get('text_user_agent');
      $data['text_accept_language'] = $this->language->get('text_accept_language');

      $data['text_history'] = $this->language->get('text_history');
      $data['text_history_add'] = $this->language->get('text_history_add');
      $data['text_loading'] = $this->language->get('text_loading');
      $data['text_shipping_telephone'] = $this->language->get('text_shipping_telephone');

      $data['column_sku'] = $this->language->get('column_sku');
      $data['column_product'] = $this->language->get('column_product');
      $data['column_model'] = $this->language->get('column_model');
      $data['column_quantity'] = $this->language->get('column_quantity');
      $data['column_price'] = $this->language->get('column_price');
      $data['column_total'] = $this->language->get('column_total');

      $data['entry_order_status'] = $this->language->get('entry_order_status');
      $data['entry_notify'] = $this->language->get('entry_notify');
      $data['entry_override'] = $this->language->get('entry_override');
      $data['entry_comment'] = $this->language->get('entry_comment');

      $data['entry_sent_comment_to_all'] = $this->language->get('entry_sent_comment_to_all');
      $data['help_sent_comment_to_all'] = $this->language->get('help_sent_comment_to_all');

      $data['help_override'] = $this->language->get('help_override');

      $data['button_invoice_print'] = $this->language->get('button_invoice_print');
      $data['button_shipping_print'] = $this->language->get('button_shipping_print');
      $data['button_edit'] = $this->language->get('button_edit');
      $data['button_cancel'] = $this->language->get('button_cancel');
      $data['button_generate'] = $this->language->get('button_generate');
      $data['button_is_invoice_add'] = $this->language->get('button_is_invoice_add');
      $data['button_is_invoice_remove'] = $this->language->get('button_is_invoice_remove');
      $data['button_reward_add'] = $this->language->get('button_reward_add');
      $data['button_reward_remove'] = $this->language->get('button_reward_remove');
      $data['button_commission_add'] = $this->language->get('button_commission_add');
      $data['button_commission_remove'] = $this->language->get('button_commission_remove');
      $data['button_history_add'] = $this->language->get('button_history_add');
      $data['button_ip_add'] = $this->language->get('button_ip_add');

      $data['tab_history'] = $this->language->get('tab_history');
      $data['tab_additional'] = $this->language->get('tab_additional');

      $data['token'] = $this->session->data['token'];
      $data['user_id'] = $this->user->getId();
      $data['user_name'] = $this->user->getUserName();

      $url = '';

      if (isset($this->request->get['filter_order_id'])) {
        $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
      }

      if (isset($this->request->get['filter_customer'])) {
        $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
      }

      if (isset($this->request->get['filter_order_status'])) {
        $url .= '&filter_order_status=' . $this->request->get['filter_order_status'];
      }

      if (isset($this->request->get['filter_total'])) {
        $url .= '&filter_total=' . $this->request->get['filter_total'];
      }

      if (isset($this->request->get['filter_date_added'])) {
        $url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
      }

      if (isset($this->request->get['filter_date_modified'])) {
        $url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
      }

      if (isset($this->request->get['sort'])) {
        $url .= '&sort=' . $this->request->get['sort'];
      }

      if (isset($this->request->get['order'])) {
        $url .= '&order=' . $this->request->get['order'];
      }

      if (isset($this->request->get['page'])) {
        $url .= '&page=' . $this->request->get['page'];
      }

      $data['breadcrumbs'] = array();

      $data['breadcrumbs'][] = array(
        'text' => $this->language->get('text_home'),
        'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
      );

      $data['breadcrumbs'][] = array(
        'text' => $this->language->get('heading_title'),
        'href' => $this->url->link('sale/order', 'token=' . $this->session->data['token'] . $url, 'SSL')
      );

      $data['stock_out_data'] = $this->model_sale_order->getstockoutbyreferid($order_info['order_id']);

      foreach($data['stock_out_data'] as $key=>$val){
        $data['stock_out_data'][$key]['url'] = $this->url->link('sale/stock_out/view', 'token=' . $this->session->data['token'] . '&out_id=' . $val['out_id'], 'SSL');
      }

      $data['export'] = $this->url->link('sale/order/export', 'token=' . $this->session->data['token'] . '&order_id=' . (int)$this->request->get['order_id'], 'SSL');
      $data['shipping'] = $this->url->link('sale/order/shipping', 'token=' . $this->session->data['token'] . '&order_id=' . (int)$this->request->get['order_id'], 'SSL');
      $data['invoice'] = $this->url->link('sale/order/invoice', 'token=' . $this->session->data['token'] . '&order_id=' . (int)$this->request->get['order_id'], 'SSL');
      $data['export_order'] = $this->url->link('sale/order/exportOrderDetails', 'token=' . $this->session->data['token'] . '&order_id=' . (int)$this->request->get['order_id'], 'SSL');

      /*编辑订单不改订单号*/
      $data['edit_order'] = $this->url->link('sale/order/editOrder', 'token=' . $this->session->data['token'] . '&order_id=' . (int)$this->request->get['order_id'], 'SSL');
      /*编辑订单不改订单号*/

      $data['edit'] = $this->url->link('sale/order/edit', 'token=' . $this->session->data['token'] . '&order_id=' . (int)$this->request->get['order_id'], 'SSL');
      $data['cancel'] = $this->url->link('sale/order', 'token=' . $this->session->data['token'] . $url, 'SSL');

      $data['order_id'] = $order_info['order_id'];

      $data['store_name'] = $order_info['store_name'];
      $data['store_url'] = $order_info['store_url'];
      $data['sign_price'] = $order_info['sign_price'];
      $data['remarks'] = $order_info['remarks'];
      $data['conform_price'] = $order_info['conform_price'];

      if ($order_info['invoice_no']) {
        $data['invoice_no'] = $order_info['invoice_prefix'] . $order_info['invoice_no'];
      } else {
        $data['invoice_no'] = '';
      }


      $data['date_added'] = date($this->language->get('date_format_short'), strtotime($order_info['date_added']));
      $data['fullname'] = $order_info['fullname'];

      if ($order_info['customer_id']) {
        $data['customer'] = $this->url->link('customer/customer/edit', 'token=' . $this->session->data['token'] . '&customer_id=' . $order_info['customer_id'], 'SSL');
      } else {
        $data['customer'] = '';
      }

      $this->load->model('customer/customer_group');

      $customer_group_info = $this->model_customer_customer_group->getCustomerGroup($order_info['customer_group_id']);

      if ($customer_group_info) {
        $data['customer_group'] = $customer_group_info['name'];
      } else {
        $data['customer_group'] = '';
      }

      $data['email'] = $order_info['email'];
      $data['telephone'] = $order_info['telephone'];

      $data['is_pay'] = $order_info['is_pay']?'已付款':'未付款';

      $data['shipping_telephone'] = $order_info['shipping_telephone'];

      $data['shipping_method'] = $order_info['shipping_method'];
      $data['recommended_code'] = $order_info['recommended_code'];
      $data['payment_method'] = $order_info['payment_method'];

      // Payment Address
      if ($order_info['payment_address_format']) {
        $format = $order_info['payment_address_format'];
      } else {
        $format = '{fullname}' . "\n" . '{company}' . "\n" . '{address}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
      }

      $find = array(
        '{fullname}',
        '{company}',
        '{address}',
        '{city}',
        '{postcode}',
        '{zone}',
        '{zone_code}',
        '{country}'
      );

      $replace = array(
        'fullname'  => $order_info['payment_fullname'],
        'company'   => $order_info['payment_company'],
        'address'   => $order_info['payment_address'],
        'city'      => $order_info['payment_city'],
        'postcode'  => $order_info['payment_postcode'],
        'zone'      => $order_info['payment_zone'],
        'zone_code' => $order_info['payment_zone_code'],
        'country'   => $order_info['payment_country']
      );

      $data['payment_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

      // Shipping Address
      if ($order_info['shipping_address_format']) {
        $format = $order_info['shipping_address_format'];
      } else {
        $format = '{fullname}' . "\n" . '{company}' . "\n" . '{address}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
      }

      $find = array(
        '{fullname}',
        '{company}',
        '{address}',
        '{city}',
        '{postcode}',
        '{zone}',
        '{zone_code}',
        '{country}'
      );

      $replace = array(
        'fullname'  => $order_info['shipping_fullname'],
        'company'   => $order_info['shipping_company'],
        'address' => $order_info['shipping_address'],
        'city'      => $order_info['shipping_city'],
        'postcode'  => $order_info['shipping_postcode'],
        'zone'      => $order_info['shipping_zone'],
        'zone_code' => $order_info['shipping_zone_code'],
        'country'   => $order_info['shipping_country']
      );

      $data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

      // Uploaded files
      $this->load->model('tool/upload');

      $data['products'] = array();

      $this->load->model('catalog/vendor');
      $products = $this->model_sale_order->getOrderProducts($order_info['order_id']);

      $total_ship_status = 1;
      foreach ($products as $product) {
        $option_data = array();

        $options = $this->model_sale_order->getOrderOptions($this->request->get['order_id'], $product['order_product_id']);

      $product_vendor_info = M('vendor')->where(array('vproduct_id'=>$product['product_id']))->field('vendor')->find();
          if($product_vendor_info) {
           $product['vendor_id'] = $product_vendor_info['vendor'];
          }
        $vname = $this->model_catalog_vendor->getVendor($product['vendor_id']);

        foreach ($options as $option) {
          if ($option['type'] != 'file') {
            $option_data[] = array(
              'name'  => $option['name'],
              'value' => $option['value'],
              'type'  => $option['type']
            );
          } else {
            $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

            if ($upload_info) {
              $option_data[] = array(
                'name'  => $option['name'],
                'value' => $upload_info['name'],
                'type'  => $option['type'],
                'href'  => $this->url->link('tool/upload/download', 'token=' . $this->session->data['token'] . '&code=' . $upload_info['code'], 'SSL')
              );
            }
          }
        }

        $childproducts = NULL;

        if($product['product_type'] == 2){
          $childproducts = $this->model_sale_order->getchildorderproducts($product['order_product_id']);
        }

        foreach($childproducts as $key=>$val){
          $childproducts[$key]['needed_quantity'] = $val['lack_quantity'] - $val['sumquan'] < 0?0:$val['lack_quantity'] - $val['sumquan'];
        }

        $data['products'][] = array(
          'order_product_id' => $product['order_product_id'],
          'childproducts'    => $childproducts,
          'product_id'       => $product['product_id'],
          'sku'          => $product['sku'],
          'name'           => $product['name'],
          'vname'        => $vname['vendor_name'],
          'model'          => $product['model'],
          'option'         => $option_data,
          'ship_status'    => $product['ship_status'],
          'quantity'       => $product['quantity'],
          'lack_quantity'    => $product['lack_quantity'],
          'sign_quantity'    => $product['sign_quantity'],
          'needed_quantity'  => $product['lack_quantity'] - $product['sumquan'] < 0?0:$product['lack_quantity'] - $product['sumquan'],
          'price'          => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
          'total'          => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']),
          'href'           => $this->url->link('catalog/product/edit', 'token=' . $this->session->data['token'] . '&product_id=' . $product['product_id'], 'SSL')
        );
        $total_ship_status *= $product['ship_status'];
      }

      $data['vouchers'] = array();

      $data['total_ship_status'] = $total_ship_status;
      $vouchers = $this->model_sale_order->getOrderVouchers($this->request->get['order_id']);

      foreach ($vouchers as $voucher) {
        $data['vouchers'][] = array(
          'description' => $voucher['description'],
          'amount'      => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']),
          'href'        => $this->url->link('sale/voucher/edit', 'token=' . $this->session->data['token'] . '&voucher_id=' . $voucher['voucher_id'], 'SSL')
        );
      }

      $data['totals'] = array();

      $totals = $this->model_sale_order->getOrderTotals($this->request->get['order_id']);

      foreach ($totals as $total) {
        $data['totals'][] = array(
          'title' => $total['title'],
          'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
        );
      }

      $data['totals'][] = array(
        'title' => '原始价',
        'text' => $this->currency->format($order_info['ori_total'], $order_info['currency_code'], $order_info['currency_value']),
      );

      $data['comment'] = nl2br($order_info['comment']);

      $this->load->model('customer/customer');

      $data['reward'] = $order_info['reward'];

      switch ($order_info['bill_status']) {
        case '0':
          $data['is_invoice_text'] = $data['text_is_invoice_removed'];
          break;
        case '1':
          $data['is_invoice_text'] = $data['text_is_invoice_added'];
          break;
        case '2':
          $data['is_invoice_text'] = $data['text_is_invoice_passed'];
          break;
        default:
          # code...
          break;
      }
      $data['is_invoice'] = $order_info['bill_status'];

      $data['reward_total'] = $this->model_customer_customer->getTotalCustomerRewardsByOrderId($this->request->get['order_id']);

      $data['affiliate_fullname'] = $order_info['affiliate_fullname'];

      if ($order_info['affiliate_id']) {
        $data['affiliate'] = $this->url->link('marketing/affiliate/edit', 'token=' . $this->session->data['token'] . '&affiliate_id=' . $order_info['affiliate_id'], 'SSL');
      } else {
        $data['affiliate'] = '';
      }

      $data['commission'] = $this->currency->format($order_info['commission'], $order_info['currency_code'], $order_info['currency_value']);

      $this->load->model('marketing/affiliate');

      $data['commission_total'] = $this->model_marketing_affiliate->getTotalTransactionsByOrderId($this->request->get['order_id']);

      $this->load->model('localisation/order_status');

      $order_status_info = $this->model_localisation_order_status->getOrderStatus($order_info['order_status_id']);

      if ($order_status_info) {
        $data['order_status'] = $order_status_info['name'];
      } else {
        $data['order_status'] = '';
      }

      $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

      $data['order_status_id'] = $order_info['order_status_id'];

      $data['account_custom_field'] = $order_info['custom_field'];

      $finance_group = $this->config->get('config_finance_user');
      $groupId = $this->user->getGroupId();
      $data['have_permission'] = false;
      $has_pay = $order_info['is_pay'];
      if(($groupId == $finance_group)&&(!$has_pay)){
        $data['have_permission'] = true;
      }

      // Custom Fields
      $this->load->model('customer/custom_field');

      $data['account_custom_fields'] = array();

      $filter_data = array(
        'sort'  => 'cf.sort_order',
        'order' => 'ASC',
      );

      $custom_fields = $this->model_customer_custom_field->getCustomFields($filter_data);

      foreach ($custom_fields as $custom_field) {
        if ($custom_field['location'] == 'account' && isset($order_info['custom_field'][$custom_field['custom_field_id']])) {
          if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
            $custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['custom_field'][$custom_field['custom_field_id']]);

            if ($custom_field_value_info) {
              $data['account_custom_fields'][] = array(
                'name'  => $custom_field['name'],
                'value' => $custom_field_value_info['name']
              );
            }
          }

          if ($custom_field['type'] == 'checkbox' && is_array($order_info['custom_field'][$custom_field['custom_field_id']])) {
            foreach ($order_info['custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
              $custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);

              if ($custom_field_value_info) {
                $data['account_custom_fields'][] = array(
                  'name'  => $custom_field['name'],
                  'value' => $custom_field_value_info['name']
                );
              }
            }
          }

          if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
            $data['account_custom_fields'][] = array(
              'name'  => $custom_field['name'],
              'value' => $order_info['custom_field'][$custom_field['custom_field_id']]
            );
          }

          if ($custom_field['type'] == 'file') {
            $upload_info = $this->model_tool_upload->getUploadByCode($order_info['custom_field'][$custom_field['custom_field_id']]);

            if ($upload_info) {
              $data['account_custom_fields'][] = array(
                'name'  => $custom_field['name'],
                'value' => $upload_info['name']
              );
            }
          }
        }
      }

      // Custom fields
      $data['payment_custom_fields'] = array();

      foreach ($custom_fields as $custom_field) {
        if ($custom_field['location'] == 'address' && isset($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
          if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
            $custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['payment_custom_field'][$custom_field['custom_field_id']]);

            if ($custom_field_value_info) {
              $data['payment_custom_fields'][] = array(
                'name'  => $custom_field['name'],
                'value' => $custom_field_value_info['name'],
                'sort_order' => $custom_field['sort_order']
              );
            }
          }

          if ($custom_field['type'] == 'checkbox' && is_array($order_info['payment_custom_field'][$custom_field['custom_field_id']])) {
            foreach ($order_info['payment_custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
              $custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);

              if ($custom_field_value_info) {
                $data['payment_custom_fields'][] = array(
                  'name'  => $custom_field['name'],
                  'value' => $custom_field_value_info['name'],
                  'sort_order' => $custom_field['sort_order']
                );
              }
            }
          }

          if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
            $data['payment_custom_fields'][] = array(
              'name'  => $custom_field['name'],
              'value' => $order_info['payment_custom_field'][$custom_field['custom_field_id']],
              'sort_order' => $custom_field['sort_order']
            );
          }

          if ($custom_field['type'] == 'file') {
            $upload_info = $this->model_tool_upload->getUploadByCode($order_info['payment_custom_field'][$custom_field['custom_field_id']]);

            if ($upload_info) {
              $data['payment_custom_fields'][] = array(
                'name'  => $custom_field['name'],
                'value' => $upload_info['name'],
                'sort_order' => $custom_field['sort_order']
              );
            }
          }
        }
      }

      // Shipping
      $data['shipping_custom_fields'] = array();

      foreach ($custom_fields as $custom_field) {
        if ($custom_field['location'] == 'address' && isset($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
          if ($custom_field['type'] == 'select' || $custom_field['type'] == 'radio') {
            $custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($order_info['shipping_custom_field'][$custom_field['custom_field_id']]);

            if ($custom_field_value_info) {
              $data['shipping_custom_fields'][] = array(
                'name'  => $custom_field['name'],
                'value' => $custom_field_value_info['name'],
                'sort_order' => $custom_field['sort_order']
              );
            }
          }

          if ($custom_field['type'] == 'checkbox' && is_array($order_info['shipping_custom_field'][$custom_field['custom_field_id']])) {
            foreach ($order_info['shipping_custom_field'][$custom_field['custom_field_id']] as $custom_field_value_id) {
              $custom_field_value_info = $this->model_customer_custom_field->getCustomFieldValue($custom_field_value_id);

              if ($custom_field_value_info) {
                $data['shipping_custom_fields'][] = array(
                  'name'  => $custom_field['name'],
                  'value' => $custom_field_value_info['name'],
                  'sort_order' => $custom_field['sort_order']
                );
              }
            }
          }

          if ($custom_field['type'] == 'text' || $custom_field['type'] == 'textarea' || $custom_field['type'] == 'file' || $custom_field['type'] == 'date' || $custom_field['type'] == 'datetime' || $custom_field['type'] == 'time') {
            $data['shipping_custom_fields'][] = array(
              'name'  => $custom_field['name'],
              'value' => $order_info['shipping_custom_field'][$custom_field['custom_field_id']],
              'sort_order' => $custom_field['sort_order']
            );
          }

          if ($custom_field['type'] == 'file') {
            $upload_info = $this->model_tool_upload->getUploadByCode($order_info['shipping_custom_field'][$custom_field['custom_field_id']]);

            if ($upload_info) {
              $data['shipping_custom_fields'][] = array(
                'name'  => $custom_field['name'],
                'value' => $upload_info['name'],
                'sort_order' => $custom_field['sort_order']
              );
            }
          }
        }
      }

      $data['ip'] = $order_info['ip'];
      $data['forwarded_ip'] = $order_info['forwarded_ip'];
      $data['user_agent'] = $order_info['user_agent'];
      $data['accept_language'] = $order_info['accept_language'];

      // Additional Tabs
      $data['tabs'] = array();

      $this->load->model('extension/extension');

      $content = $this->load->controller('payment/' . $order_info['payment_code'] . '/order');

      if ($content) {
        $this->load->language('payment/' . $order_info['payment_code']);

        $data['tabs'][] = array(
          'code'    => $order_info['payment_code'],
          'title'   => $this->language->get('heading_title'),
          'content' => $content
        );
      }

      $extensions = $this->model_extension_extension->getInstalled('fraud');

      foreach ($extensions as $extension) {
        if ($this->config->get($extension . '_status')) {
          $this->load->language('fraud/' . $extension);

          $content = $this->load->controller('fraud/' . $extension . '/order');

          if ($content) {
            $data['tabs'][] = array(
              'code'    => $extension,
              'title'   => $this->language->get('heading_title'),
              'content' => $content
            );
          }
        }
      }

      // API login
      $this->load->model('user/api');

      $api_info = $this->model_user_api->getApi($this->config->get('config_api_id'));

      if ($api_info) {
        $data['api_id'] = $api_info['api_id'];
        $data['api_key'] = $api_info['key'];
        $data['api_ip'] = $this->request->server['REMOTE_ADDR'];
      } else {
        $data['api_id'] = '';
        $data['api_key'] = '';
        $data['api_ip'] = '';
      }

      $data['tradeno_status'] = $this->model_sale_order->gettradeNo($order_info['order_id']);
            $data['refundInfo'] = $this->model_sale_order->getOnlineRefundInfo($order_id, false);//在线退款信息
            $data['luckDrawAmount'] = $this->model_sale_order->getLuckDrawAmount($order_id, 7);//春节抽奖中奖信息，7是luckydraw表PK

      $data['header'] = $this->load->controller('common/header');
      $data['column_left'] = $this->load->controller('common/column_left');
      $data['footer'] = $this->load->controller('common/footer');

      $this->response->setOutput($this->load->view('shelf/shelf_info.tpl', $data));
    } else {
          $this->load->language('error/not_found');

          $this->document->setTitle($this->language->get('heading_title'));

          $data['heading_title'] = $this->language->get('heading_title');

          $data['text_not_found'] = $this->language->get('text_not_found');

          $data['breadcrumbs'] = array();

          $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
          );

          $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('error/not_found', 'token=' . $this->session->data['token'], 'SSL')
          );

          $data['header'] = $this->load->controller('common/header');
          $data['column_left'] = $this->load->controller('common/column_left');
          $data['footer'] = $this->load->controller('common/footer');

          $this->response->setOutput($this->load->view('error/not_found.tpl', $data));
    }
  }
//删除货架订单
  public function delshelf(){
  echo 1;die;
    $this->load->language('api/order');

    $json = array();
      $this->load->model('shelf/goodsshelf');
      $this->load->model('checkout/order)');
      if (isset($this->request->get['shelforder_id'])) {
        $shelforder_id = $this->request->get['shelforder_id'];
      } else {
        $shelforder_id = 0;
      }
      var_dump($shelforder_id);die;
      $addshelforderHistory = $this->model_shelf_goodsshelf->addshelforderHistory($shelforder_id);
      var_dump($addshelforderHistory);die;
        //   $json['success'] = $this->language->get('text_success');

        //   $json['error'] = $this->language->get('error_not_found');

        // $json['error'] = "出库单已经生成，订单不能删除";




    // if (isset($this->request->server['HTTP_ORIGIN'])) {
    //   $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
    //   $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    //   $this->response->addHeader('Access-Control-Max-Age: 1000');
    //   $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
    // }
    // // var_dump($json);die();

    // $this->response->addHeader('Content-Type: application/json');
    // $this->response->setOutput(json_encode($json));

  }



}
